﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SmartSpace.Silverlight
{
    public class VerticalSwitchEffect : SwitchEffect
    {
        private Storyboard _storyBoard;
        private VThicknessWrapper _wrapperIn;
        private VThicknessWrapper _wrapperOut;

        private TimeSpan _duration;

        public VerticalSwitchEffect()
        {
            _duration = TimeSpan.FromMilliseconds(500);
        }

        public VerticalSwitchEffect(TimeSpan duration)
        {
            _duration = duration;
        }

        protected override void OnPrepareSwitch(Size containerSize, SlView outView, SlView inView, SwitchDirection direction)
        {
            _storyBoard = new Storyboard();
            _storyBoard.Completed += new EventHandler(_storyBoard_Completed);

            if (Direction == SwitchDirection.Forward) // 向上
            {
                if (inView != null)
                {
                    // inview 从下边进入可视区域,outview从可视区域往上移动,离开可视区域
                    InView.Margin = new Thickness(0, containerSize.Height, 0, -containerSize.Height);

                    _wrapperIn = new VThicknessWrapper();
                    _wrapperIn.Target = inView;
                    _wrapperIn.PropertyName = "Margin";

                    DoubleAnimation animationIn = new DoubleAnimation();
                    animationIn.To = 0;
                    animationIn.From = containerSize.Height;
                    animationIn.Duration = _duration;
                    animationIn.RepeatBehavior = new RepeatBehavior(1);
                    animationIn.AutoReverse = false;
                    Storyboard.SetTarget(animationIn, this._wrapperIn);
                    Storyboard.SetTargetProperty(animationIn, new PropertyPath("Value"));
                    _storyBoard.Children.Add(animationIn);
                }

                if (OutView != null)
                {
                    OutView.Margin = new Thickness(0);

                    _wrapperOut = new VThicknessWrapper();
                    _wrapperOut.Target = outView;
                    _wrapperIn.PropertyName = "Margin";

                    DoubleAnimation animationOut = new DoubleAnimation();
                    animationOut.To = -containerSize.Height;
                    animationOut.From = 0;
                    animationOut.Duration = _duration;
                    animationOut.RepeatBehavior = new RepeatBehavior(1);
                    animationOut.AutoReverse = false;
                    Storyboard.SetTarget(animationOut, this._wrapperOut);
                    Storyboard.SetTargetProperty(animationOut, new PropertyPath("Value"));
                    _storyBoard.Children.Add(animationOut);
                }
            }
            else if (Direction == SwitchDirection.Backward) // 向下
            {
                if (inView != null)
                {
                    // inview 从上进入可视区域, outview从可视区域往下边移动,离开可视区域
                    InView.Margin = new Thickness(0, -containerSize.Height, 0, containerSize.Height);

                    _wrapperIn = new VThicknessWrapper();
                    _wrapperIn.Target = inView;
                    _wrapperIn.PropertyName = "Margin";

                    DoubleAnimation animationIn = new DoubleAnimation();
                    animationIn.To = 0;
                    animationIn.From = -containerSize.Height;
                    animationIn.Duration = _duration;
                    animationIn.RepeatBehavior = new RepeatBehavior(1);
                    animationIn.AutoReverse = false;
                    Storyboard.SetTarget(animationIn, this._wrapperIn);
                    Storyboard.SetTargetProperty(animationIn, new PropertyPath("Value"));
                    _storyBoard.Children.Add(animationIn);
                }

                if (OutView != null)
                {
                    OutView.Margin = new Thickness(0);

                    _wrapperOut = new VThicknessWrapper();
                    _wrapperOut.Target = outView;
                    _wrapperIn.PropertyName = "Margin";

                    DoubleAnimation animationOut = new DoubleAnimation();
                    animationOut.To = containerSize.Height;
                    animationOut.From = 0;
                    animationOut.Duration = _duration;
                    animationOut.RepeatBehavior = new RepeatBehavior(1);
                    animationOut.AutoReverse = false;
                    Storyboard.SetTarget(animationOut, this._wrapperOut);
                    Storyboard.SetTargetProperty(animationOut, new PropertyPath("Value"));
                    _storyBoard.Children.Add(animationOut);
                }
            }
        }

        void _storyBoard_Completed(object sender, EventArgs e)
        {
            FinishSwitch();
        }

        protected override void OnBegin()
        {
            _storyBoard.Begin();
        }
    }
}
